<template>
	<view>
		<!--照相机组件-->
		<camera device-position="back" flash="off" class="camera" @error="error" v-if="showCamera"></camera>
		<!--照片回显-->
		<image mode="widthFix" class="image" :src="photoPath" v-if="showImage"></image>
		<!--操作按钮-->
		<view class="operate-container">
			<button type="primary" class="btn" @tap="clickBtn">{{ btnText }}</button>
			<button type="warn" class="btn" @tap="afresh">重拍</button>
		</view>
		<view class="notice-container">
			<text class="notice">注意事项</text>
			<text class="desc">
				<block v-if="type!='driverCardBack'">
					拍照的时候，必须把证件拍摄清晰并完整，否则影响识别结果。持证拍照的时候不许戴帽子、墨镜和面纱等遮挡五官的饰物，只拍摄上半身即可。手持证件，五官与证件必须同时拍摄到照片中。</block>
				<block v-if="type=='driverCardBack'">拍照的时候，必须把驾驶证背面拍摄的清晰完整，否则影响认证结果。</block>
			</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: null,
				photoPath: '',
				btnText: '拍照',
				showCamera: true,
				showImage: false
			};
		},
		onLoad: function(options) {
			this.type = options.type;
		},
		methods: {
			clickBtn: function() {
				let that = this;
				if (that.btnText == '拍照') {
					let ctx = uni.createCameraContext();
					ctx.takePhoto({
						quality: 'high',
						success: function(resp) {
							that.photoPath = resp.tempImagePath;
							that.showCamera = false;
							that.showImage = true;
							that.btnText = '提交';
						}
					});
				} else {
					let pages = getCurrentPages(); //你访问的小程序页面历史记录
					let prevPage = pages[pages.length - 2]; //上一个小程序页面
					//调用上一个页面的updatePhoto函数，回传拍好的照片
					prevPage.$vm.updatePhoto(that.type, that.photoPath);
					//返回上一个页面
					uni.navigateBack({
						delta: 1
					});
				}
			},
			afresh: function() {
				let that = this;
				that.showCamera = true;
				that.showImage = false;
				that.btnText = '拍照';
			}
		}
	};
</script>

<style lang="less">
	@import url('identity_camera.less');
</style>
